﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Common.Generator
{
    public class TextEditorComponentGenerator : ComponentGenerator
    {
        public override string WidgetTypeName => "TextEditor";

        public override ComponentResponse DoGenerate(Component component)
        {
            var response = new ComponentResponse();
            StringBuilder strResult = new StringBuilder();
            strResult.Append($"<Form.Item label='{component.Title}' name='{component.Name}' ");
            if (component.Required)
            {
                strResult.Append("rules={[{ required: true, message: '" + component.Message + "' }]}");
            }
            strResult.Append(" >");
            var bindHtml = string.Empty;
            if (!string.IsNullOrEmpty(component.Bind))
            {
                bindHtml = $"value={{{component.Bind}}}";
            }
            // <TextEditor initialValue={article.Content} valueChange={setContent} />
            strResult.Append($" <input type='hidden' name='{component.Name}' {bindHtml} />");
            strResult.Append($"<{WidgetTypeName} initialValue='' valueChange={{set{component.Bind}}} />");
            response.ComponentHtml = strResult.ToString();
            response.VariableList = GenerateVariable(component);
            return response;
        }

        public override List<string> GenerateVariable(Component component)
        {
            var variableResult = new List<string>();
            if (!string.IsNullOrEmpty(component.Bind))
            {
                variableResult.Add($" const [{component.Bind},set{component.Bind}] = useState<any>(); ");

            }
            return variableResult;
        }
    }
}
